package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.StringUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.common.client.HttpFunctionConfigDto;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowFunctionConfigVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowFunctionConfigRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowFunctionConfig;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowFunctionConfigDAO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;


@Slf4j
@Repository
public class FlowFunctionConfigRepositoryImpl implements FlowFunctionConfigRepository {

    @Autowired
    FlowFunctionConfigDAO flowFunctionConfigDAO;


    private FlowFunctionConfig convert(FlowFunctionConfigVo flowFunctionConfigVo) {
        if (Objects.isNull(flowFunctionConfigVo)) {
            return null;
        }
        FlowFunctionConfig flowFunctionConfig = new FlowFunctionConfig();
        BeanUtils.copyProperties(flowFunctionConfigVo, flowFunctionConfig);
        return flowFunctionConfig;
    }

    private FlowFunctionConfigVo convert(FlowFunctionConfig flowFunctionConfig) {
        if (Objects.isNull(flowFunctionConfig)) {
            return null;
        }

        FlowFunctionConfigVo result = new FlowFunctionConfigVo();
        BeanUtils.copyProperties(flowFunctionConfig, result);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveFunctionConfig(HttpFunctionConfigDto customFunctionConfigDto) {
        FlowFunctionConfigVo flowFunctionConfigVo = new FlowFunctionConfigVo();
        flowFunctionConfigVo.setFunctionName(customFunctionConfigDto.getFunctionName());
        flowFunctionConfigVo.setFunctionUrl(customFunctionConfigDto.getFunctionUrl());
        flowFunctionConfigVo.setFunctionParameters(customFunctionConfigDto.getFunctionParameters());
        // 更新
        if (StringUtils.isNotEmpty(customFunctionConfigDto.getFunctionCode()) && !Objects.isNull(getFunctionConfInfoByFunCode(customFunctionConfigDto.getFunctionCode()))) {
            flowFunctionConfigVo.setFunctionCode(customFunctionConfigDto.getFunctionCode());
            flowFunctionConfigDAO.updateFunctionConfig(this.convert(flowFunctionConfigVo));
        } else {
            // 新增
            flowFunctionConfigVo.setFunctionCode(UUID.randomUUID().toString().replace("-", "").toUpperCase());
            flowFunctionConfigVo.setCallMethod("POST");
            flowFunctionConfigDAO.saveFunctionConfig(this.convert(flowFunctionConfigVo));
        }
    }

    @Override
    public boolean deleteFunctionConfig(List<Integer> ids) {
        if (ids == null || ids.size() == 0) {
            return false;
        }
        int k = flowFunctionConfigDAO.deleteFunctionConfig(ids);
        if (k > 0) {
            log.info("自定义函数删除成功");
            return true;
        } else {
            log.info("自定义函数删除失败");
        }
        return false;
    }

    @Override
    public List<FlowFunctionConfigVo> getAllFunctionConfigList() {
        List<FlowFunctionConfig> flowFunctionConfigList = flowFunctionConfigDAO.getAllFunctionConfigList();
        List<FlowFunctionConfigVo> flowFunctionConfigVoList = flowFunctionConfigList.stream().map(this::convert).collect(Collectors.toList());
        return flowFunctionConfigVoList;
    }

    @Override
    public List<FlowFunctionConfigVo> getFunctionConfigByFunName(HttpFunctionConfigDto customFunctionConfigDto) {
        List<FlowFunctionConfig> flowFunctionConfigList = flowFunctionConfigDAO.getFunctionConfigByFunName(customFunctionConfigDto.getFunctionName());
        List<FlowFunctionConfigVo> flowFunctionConfigVoList = flowFunctionConfigList.stream().map(this::convert).collect(Collectors.toList());
        return flowFunctionConfigVoList;
    }

    @Override
    public FlowFunctionConfigVo getFunctionConfInfoByFunId(String funId) {
        return convert(flowFunctionConfigDAO.getFlowFunctionConfigInfoByFunctionId(funId));
    }

    @Override
    public List<FlowFunctionConfigVo> queryFunctionConfig(HttpFunctionConfigDto customFunctionConfigDto) {
        return flowFunctionConfigDAO.getAllFunctionConfigList().stream()
                .filter(flowFunctionConfig -> {
                    if (StringUtils.isNotEmpty(customFunctionConfigDto.getFunctionName()) &&
                            !flowFunctionConfig.getFunctionName().equals(customFunctionConfigDto.getFunctionName())) {
                        return false;
                    }
                    if (StringUtils.isNotEmpty(customFunctionConfigDto.getFunctionUrl()) &&
                            !flowFunctionConfig.getFunctionUrl().equals(customFunctionConfigDto.getFunctionUrl())) {
                        return false;
                    }
                    return true;
                })
                .map(this::convert)
                .collect(Collectors.toList());
    }
    public FlowFunctionConfigVo getFunctionConfInfoByFunCode(String funCode) {
        return convert(flowFunctionConfigDAO.getFlowFunctionConfigInfoByFunctionCode(funCode));
    }

}
